package org.bangumibuddy.utils;

import java.time.Duration;

public class CommonUtils {
    public static String formatSize(long bytes) {
        if(bytes == 0) return "0 B"; // 0 bytes should be 0 B (not 0.00 B

        String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
        int unitIndex = (int) (Math.log10(bytes) / 3);
        double unitValue = bytes / Math.pow(1024, unitIndex);
        return String.format("%.2f %s", unitValue, units[unitIndex]);
    }

    public static String formatTime(long seconds) {
        Duration duration = Duration.ofSeconds(seconds);
        return duration.toHoursPart() + ":" + String.format("%02d", duration.toMinutesPart()) + ":" + String.format("%02d", duration.toSecondsPart());
    }
}
